package org.aperture.exploit;

import java.io.*;
import java.security.*;

public class Hash {
	
//	public static void main(String[] args) throws Exception {
////		String path = args[0];
//		String path = "E:/Media/Music/mp3 conversion/";
//		String file;
//		File folder = new File(path);
//		File[] fileList = folder.listFiles();
//		
//		System.out.print("Filename:\tMD5:\n");
//		
//		for (int i = 0; i < fileList.length; i++) {
//			if (fileList[i].isFile()) {
//				file = fileList[i].getPath();
//				System.out.print(file + "\t" + getHashHex(file, "MD5") + "\t" + getHashHex(file, "SHA-256") + "\n");
//			}
//		}
//	}
	
	public static byte[] getHash(String filename, String algorithm) throws Exception {
		InputStream fileInput = null;
		fileInput = new FileInputStream(filename);
		MessageDigest md = MessageDigest.getInstance(algorithm);
		byte[] buffer = new byte[1024];
		try {
			int numRead;
			do {
				numRead = fileInput.read(buffer);
				if (numRead > 0) {
					md.update(buffer, 0, numRead);					
				}
			} while (numRead != -1);
			fileInput.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
            if (fileInput != null) {
                fileInput.close();
            }
		}
		return md.digest();
	}
	
//	public static String getHashHex(String filename, String algorithm) throws Exception {
//		try {
//			byte[] b = getHash(filename, algorithm);
//			String result = "";
//			for (int i=0; i < b.length; i++) {
//				result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
//			}
//			return result.toUpperCase();
//		} catch (Exception e) {
//			e.toString();
//		}
//		return "No go.";
//	}

	public static String getHashHex(String filename, String algorithm) throws Exception {
		byte[] b = getHash(filename, algorithm);
		String result = "";
		for (int i=0; i < b.length; i++) {
			result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
		}
		return result.toUpperCase();
	}
	
	
}
